﻿package classes
{
	import flash.display.MovieClip;
	import flash.events.Event;
	import flash.net.URLRequest;
	import flash.display.Loader;
	import flash.events.Event;
	import flash.events.ProgressEvent;
	import flash.display.Shape;
	import flash.events.IOErrorEvent;
	
	public class loadExternalSWF extends MovieClip 
	{
		private var mLoader:Loader;
		private var mRequest:URLRequest;
		private var preloadShape:Shape;
		
		public function loadExternalSWF(_url:String):void 
		{
			preloadShape = new Shape();
			addChild(preloadShape);
			
			mLoader = new Loader();
			mRequest = new URLRequest(_url);
			mLoader.contentLoaderInfo.addEventListener(Event.COMPLETE, onCompleteHandler);
			mLoader.contentLoaderInfo.addEventListener(ProgressEvent.PROGRESS, onProgressHandler);
			mLoader.contentLoaderInfo.addEventListener(IOErrorEvent.IO_ERROR, onErrorHandler);
			mLoader.load(mRequest);
		}
		
		private function onProgressHandler(e:ProgressEvent):void
		{
			trace("SWF % loaded: " + 100*e.bytesLoaded / e.bytesTotal);
			
			preloadShape.graphics.clear();
			preloadShape.graphics.lineStyle(1);
			preloadShape.graphics.drawRect(10, 10, 110, 5);
			preloadShape.graphics.beginFill(0);
			preloadShape.graphics.drawRect(10, 10, 10 + (e.bytesLoaded / e.bytesTotal) * 100, 5);
			preloadShape.graphics.beginFill(0x0000FF);
			preloadShape.graphics.endFill();
		}
		
		private function onCompleteHandler(loadEvent:Event):void
		{
			removeChild(preloadShape);
			addChild(loadEvent.currentTarget.content);
			mLoader.contentLoaderInfo.removeEventListener(Event.COMPLETE, onCompleteHandler);
			mLoader.contentLoaderInfo.removeEventListener(ProgressEvent.PROGRESS, onProgressHandler);
			mLoader.contentLoaderInfo.removeEventListener(IOErrorEvent.IO_ERROR, onErrorHandler);
		}
		
		private function onErrorHandler(errorEvent:Event):void 
		{
			trace("file not found")
		}
		
	}
	
}